import hashlib
from utils.db import db


class AuthController:
    def __init__(self):
        self.current_user = None

    def login(self, username, password):
        """
        验证用户登录
        返回元组 (是否成功, 消息)
        """
        # 对密码进行MD5加密
        hashed_password = hashlib.md5(password.encode()).hexdigest()

        # 查询用户
        sql = "SELECT * FROM users WHERE username = ? AND password = ?"
        user = db.fetch_one(sql, (username, hashed_password))

        if user:
            self.current_user = dict(user)
            return True, "登录成功"
        return False, "用户名或密码错误"

    def get_current_user(self):
        """获取当前登录用户信息"""
        return self.current_user

    def logout(self):
        """退出登录"""
        self.current_user = None
